package il.adscan.scan.impl;

import com.gargoylesoftware.htmlunit.html.HtmlPage;
import il.adscan.domain.Advertisement;
import il.adscan.domain.AdvertisementFilter;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class Yad2Scanner extends BaseScanner {

    public static final String BASE_URL = "http://www.yad2.co.il";

    @Override
    protected HtmlPage getPage(int pageNumber, String baseURI) throws IOException {
        String delimiter = getParametersDelimiter(baseURI);
        //http://www.yad2.co.il/Nadlan/sales.php?Page=1
        return getHTMLPage(String.format("%s%sPage=%s", baseURI, delimiter, pageNumber));
    }

    @Override
    protected Advertisement getAdvertisementFromLink(String link) throws IOException {
        HtmlPage htmlPage = getNewHTMLPage(link);
        //Click show cell number and name anchor
        String originalAdvertisementId = getOriginalIdFromLink(link);
//        DomElement elementById = htmlPage.getElementById("ancPhoneNumber_" + originalAdvertisementId);
//        HtmlAnchor showCellNumberAnchor = (HtmlAnchor) elementById;
//        htmlPage = showCellNumberAnchor.click();
//        waitForScript(200);

        Document doc = Jsoup.parse(htmlPage.asXml());
        //.FRight:not([id])
        Elements textElements = doc.select(".details_block_296 .innerDetailsDataGrid tr");
        Elements imgElements = doc.select(".FLeft");
        Elements priceElements = doc.select(".PriceData");
        Elements extraInfoElements = doc.select(".ExNoteInfo");
        Element contactNameElement = doc.getElementById("ctl00_PageContentPlaceHolder_innerAdContent_innerAdContact_lblContactName");
        Element phoneNumberElement = doc.getElementById("spnPhoneNumber_" + originalAdvertisementId);
        Element phoneNumber2Element = doc.getElementById("spnPhone2_" + originalAdvertisementId);

        Advertisement advertisement = new Advertisement();
        advertisement.setSite(getSiteURL());
        advertisement.setOriginalId(originalAdvertisementId);
        advertisement.setPropertyType(getElementValue(textElements.get(0)));
        advertisement.setSubarea(getElementValue(textElements.get(1)));
        advertisement.setCity(getElementValue(textElements.get(2)));
        advertisement.setNeighborhood(getElementValue(textElements.get(3)));
        advertisement.setAddress(getElementValue(textElements.get(4)));
        advertisement.setRoom(getElementValue(textElements.get(5)));
        advertisement.setFloor(getElementValue(textElements.get(6)));//Need to roll out child nodes and concatenate Element html
        advertisement.setStatus(getElementValue(textElements.get(7)));
        advertisement.setAvailableDate(getElementValue(textElements.get(8)));//"Login" field
        advertisement.setSquareMetersSize(getElementValue(textElements.get(9)));//Maybe! Need to roll out child nodes and concatenate Element html

        advertisement.setPrice(StringUtils.trim(priceElements.get(0).childNodes().get(2).toString()));

        advertisement.setParking(isAvailable(imgElements.get(0)));
        advertisement.setAir(isAvailable(imgElements.get(1)));
        advertisement.setSecurityRoom(isAvailable(imgElements.get(2)));
        advertisement.setElevator(isAvailable(imgElements.get(3)));
        advertisement.setHasBalcony(isAvailable(imgElements.get(4)));
        advertisement.setAccessForDisabled(isAvailable(imgElements.get(5)));
        //advertisement.setAccessForDisabled(isAvailable(imgElements.get(6)));// What is ????? ??????
        advertisement.setExtras(getElementValue(extraInfoElements.get(0)));
        advertisement.setUserName(contactNameElement.html());
        if (phoneNumberElement != null && phoneNumber2Element != null) {
            advertisement.setUserPhone(phoneNumberElement.html());
            advertisement.setUserPhone2(phoneNumber2Element.html());
        }

        return advertisement;
    }

    @Override
    protected String getOriginalIdFromLink(String link) {
        int beginIndex = link.indexOf("NaldanID=");
        return link.substring(beginIndex);
    }

    @Override
    protected AdScraper getAdScraper(Element element) {
        return new AdScraperYad2(element, this);
    }

    @Override
    protected Elements getElements(Document doc) {
        return doc.select("#main_table .main_table [id*=tr_Ad_2_1_]");
    }

    @Override
    protected boolean hasNextPage(HtmlPage htmlPage) {
        return false;
    }

    @Override
    protected boolean isProcessCurrentPage(HtmlPage htmlPage) {
        return true;
    }

    @Override
    protected HtmlPage applyFilters(HtmlPage htmlPage, AdvertisementFilter filter) {
        return htmlPage;
    }

    @Override
    protected boolean isAvailable(Element element) {
        return true;
    }

    @Override
    public String getSiteURL() {
        return BASE_URL + "/Nadlan/sales.php";
    }
}
